export default class {
    constructor() {
        routerArray.push({
            path: "/",
            redirect: "/home"
                // name: "Home",
                // meta: { requiresAuth: true },//可以配置路由mate,具体用法见路由导航beforeEach实例
                // component: () =>
                //     import ("../pages/blog/home.vue")
        });
        register("Home", () =>
            import ("../pages/blog/home.vue"), [
                child("Index", () =>
                    import ("../pages/blog/index.vue"), true),
                child("LeaveMsg", () =>
                    import ("../pages/blog/leavemsg.vue"), true),
                child("AboutMe", () =>
                    import ("../pages/blog/aboutme.vue"), true),
            ]
        );
    }

    getRouterArray() {
        return routerArray;
    }
}

const routerArray = [];

function register(name, component, array, keepAlive) {
    let path = "/" + getPath(name);
    let obj = {
        path: path,
        name: name,
        component: component
    };
    if (keepAlive) {
        obj.meta = {
            keepAlive: true
        };
    }
    if (array) {
        for (let i = 0; i < array.length; i++) {
            let item = array[i];
            item.path = path + "/" + getPath(item.name);
        }
        obj.children = array;
    }
    routerArray.push(obj);
}

function child(name, component, keepAlive) {
    let obj = {
        name: name,
        component: component
    };
    if (keepAlive) {
        obj.meta = {
            keepAlive: true
        };
    }
    return obj;
}

function getPath(name) {
    let str = "";
    for (let i = 0; i < name.length; i++) {
        let item = name.charAt(i);
        if (item == "_") {
            item = "-";
        } else if (/^[A-Z]+$/.test(item)) {
            item = item.toLocaleLowerCase();
            if (str.length > 0) {
                item = "-" + item;
            }
        }
        str += item;
    }
    return str;
}